package valuecomusa.fijibus.eticket;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import valuecomusa.fijibus.eticket.shorthaul.R;

// import android.view.Menu;

public class Cash_TopUp extends Activity
{
	private double Amount;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cash_topup);
        //---
        setResult(RESULT_CANCELED, null);
        //---
		TextView tvtmp = (TextView) findViewById(R.id.tvCustID);
		tvtmp.setText(WorkingSession.getInstance().currentPassenger.UserName + " ("
				+ WorkingSession.getInstance().currentPassenger.getFullName() + ")");
		//--
		Amount = WorkingSession.getInstance().RoundMoney(getIntent().getDoubleExtra("FARE_AMOUNT", 0));
        Amount = Amount - WorkingSession.getInstance().currentPassenger.CurrentBalance;
        Amount = Amount < 0 ? 0 : Amount;

		//Amount = Math.round(Amount * 100);
		//Amount = Amount / 100;

		//--
		tvtmp = (TextView)findViewById(R.id.cash_topup_min_value_label);
		tvtmp.setText(String.format("(minimum %s)", WorkingSession.getInstance().decimalFormat.format(Amount)));
		
		EditText et = (EditText) findViewById(R.id.cash_topup_amount);
		et.setFilters(new InputFilter[] { new InputFilterMinMax(0, 1000f) });
		
		Button submit = (Button) findViewById(R.id.btSumit);
		submit.setOnClickListener(onSubmit);
        //--
        Button cancel = (Button) findViewById(R.id.cash_topup_cancel);
        cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                finish();
            }
        });
	}

	View.OnClickListener onSubmit = new View.OnClickListener()
	{

		@Override
		public void onClick(View v)
		{
            InputMethodManager inputManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);

            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
            //--
			EditText et = (EditText)findViewById(R.id.cash_topup_amount);
			if(et.getText().toString().length() == 0)
			{

                ShowError("Please enter amount");
				return;
			}
			
			double inputValue = Double.parseDouble(et.getText().toString());
			if(inputValue < Amount )
			{
                ShowError(
						String.format("Amount must be greater than %s", WorkingSession.getInstance().decimalFormat.format(Amount))
                );
				return;
			}

            Intent intent = new Intent();
            intent.putExtra("CASH_TOPUP_AMOUNT", inputValue);
            setResult(RESULT_OK, intent);
			finish();

		}
	};

    private void ShowError(String msg)
    {
        TextView errorText = (TextView) findViewById(R.id.cash_topup_errorText);
        errorText.setText(msg);
    }

}
